using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace bootstrapSP.Features.bootstrapSP_Master_Page
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("d37a5929-6a89-4028-91d2-7ac393960c80")]
    public class bootstrapSP_Master_PageEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            
           // var site = (SPSite)properties.Feature.Parent;

           // // Convert the html to master page
           // SPList masterPageCatalog = site.RootWeb.GetCatalog(SPListTemplateType.MasterPageCatalog);    
           //SPFile htmlNeedToConvert = site.RootWeb.GetFile(site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/bootstrapSP/bootstrap3.html");
           //htmlNeedToConvert.RevertContentStream();
           //htmlNeedToConvert.CheckOut();
           //SPListItem masterPageItem = htmlNeedToConvert.Item;
           //masterPageItem["ContentTypeId"] = "0x0101000F1C8B9E0EB4BE489F09807B2C53288F0054AD6EF48B9F7B45A142F8173F171BD10003D357F861E29844953D5CAA1D4D8A3A";
           //masterPageItem["HtmlDesignAssociated"] = "1";           
           //masterPageItem.Update();
           //htmlNeedToConvert.CheckIn("Convert html to master page by FeatureActivated");
           //htmlNeedToConvert.Publish("Convert html to master page by FeatureActivated");        
           // site.RootWeb.MasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/bootstrapSP/bootstrap3.master";
           // site.RootWeb.CustomMasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/bootstrapSP/bootstrap3.master";
           // site.RootWeb.Update();

           // // string[] pageLayouts = { "banded.html", "banner.html", "blog.html", "orbit.html", "sidebar.html", "workspace.html" };
           // string[] pageLayouts = { "banded.html",  "blog.html", "orbit.html", "sidebar.html", "workspace.html" };
           // this.PublishPageLayouts(pageLayouts, site.RootWeb);
        }

        
        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var site = (SPSite)properties.Feature.Parent;
            site.RootWeb.MasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/seattle.master";
            site.RootWeb.CustomMasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/seattle.master";
            site.RootWeb.Update();
        }

        private void PublishPageLayouts(string[] pageLayousts, SPWeb web)
        {
            foreach (string pageLayoutPath in pageLayousts)
            {
                SPFile htmlNeedToConvert = web.GetFile(web.ServerRelativeUrl + "_catalogs/masterpage/bootstrapSP/" + pageLayoutPath);
                htmlNeedToConvert.RevertContentStream();
                htmlNeedToConvert.CheckOut();
                SPListItem pageLayoutItemItem = htmlNeedToConvert.Item;
                pageLayoutItemItem["Description"] = "Convert html to page layout by FeatureActivated";            
                pageLayoutItemItem.Update();
                htmlNeedToConvert.CheckIn("Convert html to page layout by FeatureActivated");
                htmlNeedToConvert.Publish("Convert html to page layout by FeatureActivated");

            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
